from django.db import models
from django.utils import timezone
from company.models import UserProfile


# AI对话会话表
class ChatSession(models.Model):
    user_profile = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name='chat_sessions',
        verbose_name='用户',
        db_comment='用户ID'
    )
    title = models.CharField(max_length=200, verbose_name='会话标题', db_comment='会话标题')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        db_table = 'mentorx_chat_sessions'
        verbose_name = 'AI对话会话'
        verbose_name_plural = 'AI对话会话'
        ordering = ['-updated_at']
        db_table_comment = 'AI对话会话表'

    def __str__(self):
        return f"{self.user_profile.user.username} - {self.title}"


# AI对话消息表
class ChatMessage(models.Model):
    ROLE_CHOICES = [
        ('user', '用户'),
        ('assistant', 'AI助手'),
    ]

    session = models.ForeignKey(
        ChatSession,
        on_delete=models.CASCADE,
        related_name='messages',
        verbose_name='所属会话',
        db_comment='所属会话ID'
    )
    role = models.CharField(max_length=20, choices=ROLE_CHOICES, verbose_name='角色', db_comment='角色:user用户,assistant AI助手')
    content = models.TextField(verbose_name='消息内容', db_comment='消息内容')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_chat_messages'
        verbose_name = 'AI对话消息'
        verbose_name_plural = 'AI对话消息'
        ordering = ['created_at']
        db_table_comment = 'AI对话消息表'

    def __str__(self):
        return f"{self.session.title} - {self.get_role_display()}"


# AI推荐任务表
class RecommendedAction(models.Model):
    STATUS_CHOICES = [
        ('pending', '待处理'),
        ('in_progress', '进行中'),
        ('completed', '已完成'),
        ('ignored', '已忽略'),
    ]

    user_profile = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name='recommended_actions',
        verbose_name='用户',
        db_comment='用户ID'
    )
    title = models.CharField(max_length=200, verbose_name='任务标题', db_comment='任务标题')
    description = models.TextField(verbose_name='任务描述', db_comment='任务描述')
    priority = models.IntegerField(default=1, verbose_name='优先级', db_comment='优先级')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态', db_comment='状态:pending待处理,in_progress进行中,completed已完成,ignored已忽略')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        db_table = 'mentorx_recommended_actions'
        verbose_name = 'AI推荐任务'
        verbose_name_plural = 'AI推荐任务'
        ordering = ['-priority', '-created_at']
        db_table_comment = 'AI推荐任务表'

    def __str__(self):
        return self.title
